﻿var findUserVM = {
    ID: ko.observable(),
    SocialID: ko.observable(),
    Name: ko.observable(),
    Birthday: ko.observable(),
    Male: ko.observable(),
    Address: ko.observable(),
    Phone: ko.observable(),
    loadRequest: null,
    cancelLoadRequest: function () {
        if (this.loadRequest != null) {
            this.loadRequest.abort();
        }
    },
    loadPatientInfo: function (searchID, searchType, indexController) {
        var self = this;
        indexController.hideResult();
        indexController.enableSearch(false);
        indexController.message.processing("processing");
        self.loadRequest = $.ajax({
            type: 'post',
            url: home + '/' + userType + '/GetPatientInfo',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ id: searchID, type: searchType }),
            success: function (data) {
                self.ID(data.ID);
                self.SocialID(data.SocialID);
                self.Name(data.Name);
                self.Birthday(data.Birthday);
                self.Male(data.Male);
                self.Address(data.Address);
                self.Phone(data.Phone);
                indexController.displayUserInfoForm(true);
                indexController.message.processComplete("process_complete");
            },
            error: function (err) {
                if (err.status == 200) {
                    indexController.message.processComplete(err.responseText);
                    if (searchType == 2 && err.responseText == "no_matched_data") {//search with Social ID
                        indexController.displayFindResultForm(true);
                    }
                }
                else {
                    indexController.message.processComplete("process_error");
                }
            },
            complete: function (xhr, textStatus) {
                indexController.enableSearch(true);
                indexController.searchTerm.focus();
            }
        });
    },
    resetPassword: function (indexController) {
        var self = this;
        indexController.enableSearch(false);
        indexController.activeSideBar(false);
        indexController.enableUserInfoButton(false);
        indexController.enableBtnCreateUserCreate(false);
        indexController.enableBtnCreateUserClear(false);
        indexController.message.processing("processing");
        $.ajax({
            type: 'post',
            url: home + '/' + userType + '/ResetPassword',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ userID: self.ID() }),
            success: function (data) {
            },
            error: function (err) {
                var p = this;
                if (err.status == 200) {
                    indexController.message.processComplete(err.responseText);
                }
                else {
                    indexController.message.processComplete("reset_pass_fail");
                }
            },
            complete: function (xhr, textStatus) {
                indexController.enableSearch(true);
                indexController.activeSideBar(true);
                indexController.enableUserInfoButton(true);
                indexController.enableBtnCreateUserCreate(true);
                indexController.enableBtnCreateUserClear(true);
            }
        });
    }
};